1 /***
2 * ClassLoader
3 *
4 * This class instruments code according
5 * to the Thomas Ball 1994 Branch Profiling
6 * algorithm.
7 */
8
9 package junit.quilt.cover.ball94;
10
11 import java.net.URL;
12
13 import java.util.Set;
14 import java.util.Map;
15 import java.util.List;
16 import java.util.HashSet;
17 import java.util.HashMap;
18 import java.util.Iterator;
19 import java.util.ArrayList;
20 import java.lang.ClassLoader;
21
22 import junit.quilt.framework.*;
23 import junit.quilt.cover.generic.*;
24 import junit.quilt.exception.*;
25
26 import cern.colt.matrix.*;
27 import cern.colt.matrix.impl.*;
28
29 import org.apache.bcel.*;
30 import org.apache.bcel.generic.*;
31 import org.apache.bcel.classfile.*;
32
33 import org.apache.commons.graph.*;
34 import org.apache.commons.graph.algorithm.spanning.*;
35
36 public class B94ClassLoader
37 extends MethInstClassLoader
38 {
39 private B94Registry registry;
40
41 public B94ClassLoader( String packages[],
42 URL path[],
43 ClassLoader parent,
44 B94Registry registry) {
45 super( packages, path, parent );
46 this.registry = registry;
47 }
48
49 public void instrumentMethod( InstContext context,
50 ControlFlowGraph graph )
51 throws QuiltException {
52
53 FlowControlEdge dummy =
54 getEdgeFactory().makeDummyEdge( graph.getEndVertex(),
55 graph.getStartVertex() );
56 graph.addEdge( dummy );
57
58
59 MinimumSpanningForest msf =
60 new MinimumSpanningForest( graph );
61
62 List chords = new ArrayList( msf.getChords() );
63 List vList = new ArrayList( graph.getVertices() );
64 List eList = new ArrayList( graph.getEdges() );
65
66 int stats[] = new int[chords.size()];
67
68 DoubleMatrix2D matrix =
69 makeMatrix( graph, chords, vList, eList );
70
71 registry.bind( context.getClassGen().getClassName(),
72 graph.getMethodName(),
73 new B94Collector( vList, eList, matrix, stats ));
74
75 int fieldRef = addStaticField( context,
76 new ArrayType( Type.INT, 1),
77 stats );
78
79 Iterator c = chords.iterator();
80 short pos = 0;
81 while (c.hasNext()) {
82 FlowControlEdge chord = (FlowControlEdge) c.next();
83 instrumentEdge(context,
84 graph,
85 chord,
86 new ArrayIncrement(fieldRef, pos));
87 pos++;
88 }
89
90 }
91
92 /***
93 * Create a matrix which will get run through
94 * the LUDecomposition to make it easy to find
95 * the number of times an edge/vertex was visited.
96 * <pre>
97 * V = | Vertices |
98 * E = | Edges |
99 * I = | Instrumented Edge Set |
100 *
101 * For Vertices v0, v1, v2, ... vV
102 * And Edges e0, e1, e2, ... eE
103 * And Instrumented Edges i0, i1, i2, ... iI
104 * (where iX = eX, X<I)
105 * And Instrumented Edge Values l0, l1, l2, ... lI
106 *
107 * e0 e1 e2 ... eI ... eE Val
108 * i0 1 l0
109 * i1 1 l1
110 * . . .
111 * . . .
112 * . . .
113 * iI 1 lI
114 * v0 1 -1 0
115 * v1 -1 1 0
116 * v2 -1 1 1 0
117 * .
118 * .
119 * .
120 * vV 1 -1
121 *
122 * For each cell:
123 * cell(iH, eD) = 1 (H < I, D < E, iH = eD)
124 * cell(iN, Val) = lN (N < I) // Filled in when Queried.
125 *
126 * cell(vU, eD) = 1 (U < V,
127 * D < E,
128 * if source(eD) == vU)
129 * cell(vU, eD) = -1 (U < V,
130 * D < E,
131 * if target(eD) == vU)
132 * </pre>
133 */
134 DoubleMatrix2D makeMatrix( DirectedGraph graph,
135 List chords,
136 List vertices,
137 List edges ) {
138 DoubleMatrix2D RC =
139 new DenseDoubleMatrix2D(vertices.size() +
140 chords.size(),
141 edges.size());
142
143 int colPos = 0;
144 int rowPos = 0;
145
146 Iterator iChords = chords.iterator();
147 while (iChords.hasNext()) {
148 Edge chord = (Edge) iChords.next();
149 colPos = edges.lastIndexOf( chord );
150
151 RC.set( rowPos, colPos, 1.0 );
152 rowPos ++;
153 }
154
155 Iterator iVerts = vertices.iterator();
156 while (iVerts.hasNext()) {
157 Vertex v = (Vertex) iVerts.next();
158
159 Iterator iInbound = graph.getInbound(v).iterator();
160 while (iInbound.hasNext()) {
161 colPos = edges.lastIndexOf( iInbound.next() );
162 RC.set( rowPos, colPos, 1.0 );
163 }
164
165 Iterator iOutbound = graph.getOutbound(v).iterator();
166 while (iOutbound.hasNext()) {
167 colPos = edges.lastIndexOf( iOutbound.next() );
168 RC.set( rowPos, colPos, -1.0 );
169 }
170 rowPos++;
171 }
172
173 return RC;
174 }
175 }
176
177
178
179
180
181
This page was automatically generated by Maven